using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace SoftwareMonkeys.Scriptech.Projects
{
	public class ProjectAssemblyReader
	{
		public static ProjectAssembly GetAssembly (string storeName, string groupName, string projectName, string assemblyName)
		{
			Project project = ProjectReader.GetProject(storeName, groupName, projectName);
			
			string assembliesDirectoryPath = ProjectAssemblyFileNamer.CreateAssembliesDirectoryPath(storeName, groupName, projectName, project.AssembliesDirectory);
			
			ProjectAssembly assembly = null;
			
			foreach (string file in Directory.GetFiles(assembliesDirectoryPath, assemblyName + ".assembly", SearchOption.AllDirectories))
			{
				assembly = GetAssemblyFromFile(file);
			}
			
			return assembly;
			
			// TODO: Clean up
			
			/*ProjectAssemblyCollection assemblies = new ProjectAssemblyCollection();
			
			string assembliesPath = ProjectAssemblyFileNamer.CreateAssembliesDirectoryPath(storeName, groupName, projectName, project.AssembliesDirectory);
			
			foreach (string assemblyPath in Directory.GetDirectories (assembliesPath)) {
				ProjectAssembly assembly = new ProjectAssembly();
				string name = Path.GetFileName (assemblyPath);
				
				assembly.Name = name;
				assembly.RootPath = assemblyPath;
				
				assemblies.Add (name, assembly);
			}
			
			return assemblies;*/
			
			/*ProjectAssemblyCollection assemblies = GetAssemblies(storeName, groupName, projectName);
			
			foreach (string a in assemblies.Keys)
			{
				throw new Exception(a + " - " + assemblyName);
				if (assemblyName == a)
					return assemblies[a];
			}
			
			return null;*/
			
			/*Project project = ProjectReader.GetProject(storeName, groupName, projectName);
			
			string query = assemblyName + ".assembly";
			
			ProjectAssembly assembly = null;
			
			foreach (string file in Directory.GetFiles(ProjectFileNamer.GetProjectPath(storeName, groupName, project.Name), query, SearchOption.AllDirectories))
			{				
				throw new Exception(file);
				assembly = GetAssemblyFromFile(file);
			}
			return assembly;*/
		}
		
		
		public static ProjectAssemblyCollection GetAssemblies (string storeName, string groupName, string projectName)
		{
			ProjectAssemblyCollection assemblies = new ProjectAssemblyCollection();
			
			Project project = ProjectReader.GetProject(storeName, groupName, projectName);
			
			string assembliesPath = ProjectAssemblyFileNamer.CreateAssembliesDirectoryPath(storeName, groupName, projectName, project.AssembliesDirectory);
			
			foreach (string assemblyPath in Directory.GetFiles(assembliesPath, "*.assembly", SearchOption.AllDirectories))
			{
				ProjectAssembly assembly = GetAssemblyFromFile(assemblyPath);
								
				assemblies.Add (assembly.Name, assembly);
			}
			
			return assemblies;
			
			
		}
		
		public static ProjectAssembly GetAssemblyFromFile(string filePath)
		{
			ProjectAssembly assembly = null;
			
			using (StreamReader reader = new StreamReader(File.OpenRead (filePath)))
			{
				XmlSerializer serializer = new XmlSerializer (typeof(ProjectAssembly));
				assembly = (ProjectAssembly)serializer.Deserialize(reader);
			}
			
			return assembly;
		}
	}
}

